package com.hui.zhifubao1.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hui.zhifubao1.config.AlipayConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@RestController
@Slf4j
public class Alipay {
    @GetMapping("/alipay")
    public void alipay(HttpServletResponse response) throws Exception {
        DefaultAlipayClient defaultAlipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();
        HashMap<String, Object> map = new HashMap<>();
        map.put("out_trade_no",System.currentTimeMillis());
        map.put("product_code","FAST_INSTANT_TRADE_PAY");
        map.put("total_amount","20.00");
        map.put("subject","蓝牙耳机");

        String s = JSONObject.toJSONString(map);
        log.info("---------"+s);
        alipayTradePagePayRequest.setBizContent(s);
        String body = defaultAlipayClient.pageExecute(alipayTradePagePayRequest).getBody();
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(body);
        response.getWriter().flush();
        response.getWriter().close();
    }
}
